package cn.jbolt.admin.permission;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.model.Permission;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

@CheckPermission(PermissionKey.PERMISSION)
@UnCheckIfSystemAdmin
@Path("/api/admin/permission")
public class PermissionAdminController extends JBoltApiBaseController {
	@Inject
	private PermissionService service;
	public void index(){
		renderJBoltApiRet(findModelById());
	}
	/**
	 * 数据接口
	 */
	public void datas(@Para("ofMoudle")Integer ofMoudle,@Para("topNavId")Long topNavId) {
		renderJBoltApiSuccessWithData(service.getAdminPermissionsWithLevel(ofMoudle,topNavId));
	}
	/**
	 * options数据接口
	 */
	public void options() {
		renderJBoltApiSuccessWithData(service.getAllPermissionsOptionsWithLevel());
	}

	/**
	 * options数据接口
	 */
	public void enableOptions() {
		renderJBoltApiSuccessWithData(service.getAllEnablePermissionsOptionsWithLevel());
	}

	/**
	 * 保存
	 */
	@Before(Tx.class)
	@JBoltHttpPost
	public void save(@Para("")Permission permission){
		renderJBoltApiRet(service.save(permission));
	}

	/**
	 * 更新
	 */
	@Before(Tx.class)
	@JBoltHttpPost
	public void update(@Para("")Permission permission){
		renderJBoltApiRet(service.update(permission));
	}

	/**
	 * 删除
	 */
	@Before(Tx.class)
	@JBoltHttpGet
	public void delete(){
		renderJBoltApiRet(service.delPermissionById(getId(),true));
	}

	/**
	 * 改变顺序
	 * @param id
	 * @param oriPid
	 * @param newPid
	 * @param oriRank
	 * @param newRank
	 */
	@Before(Tx.class)
	@JBoltHttpPost
	public void changeSortRank(Long id, Long oriPid, Long newPid, Integer oriRank, Integer newRank) {
		renderJBoltApiRet(service.changeSortRank(id, oriPid, newPid, oriRank, newRank));
	}

	/**
	 * 上移
	 */
	@Before(Tx.class)
	@JBoltHttpGet
	public void up(){
		renderJBoltApiRet(service.up(getId()));
	}

	/**
	 * 下移
	 */
	@Before(Tx.class)
	@JBoltHttpGet
	public void down(){
		renderJBoltApiRet(service.down(getId()));
	}

	/**
	 * 初始化顺序
	 */
	@Before(Tx.class)
	@JBoltHttpGet
	public void initRank(){
		renderJBoltApiRet(service.initRank());
	}
	/**
	 * 切换是否是超管默认
	 */
	@Before(Tx.class)
	@JBoltHttpGet
	public void toggleIsSystemAdminDefault(){
		renderJBoltApiRet(service.toggleIsSystemAdminDefault(getId()));
	}
	@Override
	protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
		return service;
	}
}
